from aiogram_dialog import DialogManager
from sqlalchemy.ext.asyncio import AsyncSession

from src.application.category.service import CategoryService


async def sites_getter(db_session: AsyncSession, dialog_manager: DialogManager, **kwargs):
    category_service: CategoryService = dialog_manager.middleware_data.get("category_service")
    category_id: int = dialog_manager.dialog_data.get("category_id")

    if not category_id and dialog_manager.start_data:
        category_id: int = dialog_manager.start_data.get("category_id")

    category = await category_service.get_category_by_id(category_id, joined=True)
    sites = category.sites if category else []

    return {
        "sites": sites,
        "count": len(sites),
    }
